/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.opshowcase.errorsreporting.client.presenter;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

import fr.openparts.OpenParts.client.OPBinder;
import fr.openparts.OpenParts.client.OPCommand;
import fr.openparts.OpenParts.client.OPCommandCallback;
import fr.openparts.OpenParts.client.OPCommandFactory;
import fr.openparts.OpenParts.client.OPCommandFailureReason;
import fr.openparts.OpenParts.client.OPParams;
import fr.openparts.OpenParts.client.OPShell;
import fr.openparts.OpenParts.client.OPSlot;
import fr.openparts.OpenParts.client.OPSlotProxy;
import fr.openparts.OpenParts.client.OPViewProxy;
import fr.openparts.OpenParts.client.annotations.OPServiceName;
import fr.openparts.OpenParts.client.annotations.OPViewFactory;
import fr.openparts.opshowcase.errorsreporting.client.view.ErrorsReportingView;

/**
 * Presenter of General Module of Errors Reporting, for OpenParts Showcase
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 30 March 2011
 */
@OPServiceName("errorsreporting")
public class ErrorsReportingPresenter implements EntryPoint {

    public interface ErrorsReportingPresenterBinder extends OPBinder<ErrorsReportingPresenter> {}
    // need to remove the static here due to a bug in the optimization done by GWT compiler (issue #6283)
    // see http://code.google.com/p/google-web-toolkit/issues/detail?id=6283
    public final ErrorsReportingPresenterBinder binder = GWT.create(ErrorsReportingPresenterBinder.class);
    
    protected final static String UNKNOWN_SERVICE = "unknownService";
    protected final static String UNKNOWN_RESOURCE = "unknownResource";
    protected final static String UNKNOWN_ACTION = "unknownAction";
    protected final static String UNKNOWN_SLOT = "unknownSlot";
    protected final static String DUMMY_SERVICE = "errorsdummyservice";
    protected final static String DUMMY_RESSOURCE = "errorsdummyresource";
    protected final static String DUMMY_EVENT = "dummyEvent";
    protected final static String DUMMY_MESSAGE = "dummyMessage";
    protected final static String DUMMY_ID = "dummyId";
    
    /**
     * Entry point, method called when the module is loaded.
     * 
     * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
     */
    @Override
    public void onModuleLoad() {
        binder.bindPart(this);
        binder.notifyLoadingCompleted();
    }

    /**
     * Returns the view for this general module
     * 
     * @param params
     *            not used here
     * @return the view for this general module
     */
    @OPViewFactory(resourceName = "errorsreporting", actionName = OPShell.ACTION_DISPLAY)
    public Widget getView(final OPParams params, final OPSlotProxy proxy) {
        return new ErrorsReportingView(this);
    }
    
    /**
     * Load dummy service view in the embedded view
     */
    public void loadSubView(final Panel viewContainer) {
        final OPSlot slot = OPShell.getSlot(DUMMY_SERVICE, "errorsembeddedresource", OPShell.ACTION_DISPLAY);
        viewContainer.add(slot);
        OPShell.displayView(DUMMY_SERVICE, "errorsembeddedresource", OPShell.ACTION_DISPLAY, slot);
    }

    /**
     * Unknown command
     */
    public void generateUnknownCommandErrorMessage(final Label label) {
        final OPCommand unknownCommand = OPCommand.commandFactory(-1, UNKNOWN_SERVICE, null, new OPCommandCallback() {
            @Override
            public void onSuccess() {
                // no way we should get here
                label.setText("Error in the test, the unknown command executed sucessfully !!! This is not what should happens !!!");
            }
            @Override
            public void onFail(OPCommandFailureReason reason) {
                label.setText(reason.getDescription());
            }
        });
        OPShell.opBindingSharedInstance().scheduleCommand(unknownCommand);
    }

    /**
     * Load timeout
     */
    public void generateLoadTimeoutErrorMessage(final Label label) {
        OPShell.loadPart(UNKNOWN_SERVICE, new OPCommandCallback() {
            @Override
            public void onSuccess() {
                // no way we should get here
                label.setText("Error in the test, unknown service loaded sucessfully !!! This is not what should happens !!!");
            }
            @Override
            public void onFail(OPCommandFailureReason reason) {
                label.setText(reason.getDescription());
            }
        });
    }
    
    /**
     * No such view - wrong resource
     */
    public void generateNoSuchView1ErrorMessage(final Label label) {
        OPShell.displayView(DUMMY_SERVICE, UNKNOWN_RESOURCE, OPShell.ACTION_DISPLAY, UNKNOWN_SLOT, new OPCommandCallback() {
            @Override
            public void onSuccess() {
                // no way we should get here
                label.setText("Error in the test, unknown resource displayed sucessfully !!! This is not what should happens !!!");
            }
            @Override
            public void onFail(OPCommandFailureReason reason) {
                label.setText(reason.getDescription());
            }
        });
    }
    
    /**
     * No such view - wrong action
     */
    public void generateNoSuchView2ErrorMessage(final Label label) {
        OPShell.displayView(DUMMY_SERVICE, DUMMY_RESSOURCE, UNKNOWN_ACTION, UNKNOWN_SLOT, new OPCommandCallback() {
            @Override
            public void onSuccess() {
                // no way we should get here
                label.setText("Error in the test, unknown action displayed sucessfully !!! This is not what should happens !!!");
            }
            @Override
            public void onFail(OPCommandFailureReason reason) {
                label.setText(reason.getDescription());
            }
        });
    }
    
    /**
     * No such slot
     */
    public void generateNoSuchSlotErrorMessage(final Label label) {
        OPShell.displayView(DUMMY_SERVICE, DUMMY_RESSOURCE, OPShell.ACTION_DISPLAY, UNKNOWN_SLOT, new OPCommandCallback() {
            @Override
            public void onSuccess() {
                // no way we should get here
                label.setText("Error in the test, unknown slot used to display view !!! This is not what should happens !!!");
            }
            @Override
            public void onFail(OPCommandFailureReason reason) {
                label.setText(reason.getDescription());
            }
        });
    }

    /**
     * Missing mandatory param.
     */
    public void generateMissingMandatoryParamErrorMessage(final Panel container, final Label label) {
        final OPSlot slot = OPShell.getSlot(DUMMY_SERVICE, DUMMY_RESSOURCE, OPShell.ACTION_DISPLAY);
        container.add(slot);
        OPShell.displayView(DUMMY_SERVICE, DUMMY_RESSOURCE, OPShell.ACTION_DISPLAY, slot, new OPCommandCallback() {
            @Override
            public void onSuccess() {
                // no way we should get here
                label.setText("Error in the test, view displayed without its mandatory param !!! This is not what should happens !!!");
                container.remove(slot);
            }
            @Override
            public void onFail(OPCommandFailureReason reason) {
                label.setText(reason.getDescription());
                container.remove(slot);
            }
        });
    }

    /**
     * No such service
     */
    public void generateNoSuchServiceErrorMessage(final Label label) {
        final OPCommand command = OPCommandFactory.dispatchEventToCommandFactory(UNKNOWN_SERVICE, DUMMY_EVENT, null);
        command.setCallback(new OPCommandCallback() {
            @Override
            public void onSuccess() {
                // no way we should get here
                label.setText("Error in the test, event dispatched to unknown service !!! This is not what should happens !!!");
            }
            @Override
            public void onFail(OPCommandFailureReason reason) {
                label.setText(reason.getDescription());
            }
        });
        OPCommandFactory.execute(command, null);
    }
    
    /**
     * Pre command failed for a display view command
     */
    public void generatePreCommandFailedViewErrorMessage(final Label label) {
        OPShell.displayView(UNKNOWN_SERVICE + "1", UNKNOWN_RESOURCE, OPShell.ACTION_DISPLAY, UNKNOWN_SLOT, new OPCommandCallback() {
            @Override
            public void onSuccess() {
                // no way we should get here
                label.setText("Error in the test, unknown service loaded sucessfully !!! This is not what should happens !!!");
            }
            @Override
            public void onFail(OPCommandFailureReason reason) {
                label.setText(reason.getDescription());
            }
        });
    }
    
    /**
     * Pre command failed for a dispatch event to command
     */
    public void generatePreCommandFailedEventErrorMessage(final Label label) {
        OPShell.dispatchEventTo(UNKNOWN_SERVICE + "2", DUMMY_EVENT, new OPCommandCallback() {
            @Override
            public void onSuccess() {
                // no way we should get here
                label.setText("Error in the test, unknown service loaded sucessfully !!! This is not what should happens !!!");
            }
            @Override
            public void onFail(OPCommandFailureReason reason) {
                label.setText(reason.getDescription());
            }
        });
    }

    /**
     * No such slot for a dispatch to view message
     */
    public void generateNoSuchSlotIdInThePageViewErrorMessage(final Label label) {
        final OPViewProxy proxy = OPShell.getSlot(UNKNOWN_SLOT).getViewProxy();
        proxy.dispatchMessageToEmbeddedView(DUMMY_MESSAGE, null, new OPCommandCallback() {
            @Override
            public void onSuccess() {
                // no way we should get here
                label.setText("Error in the test, OPViewProxy found an unknown slot !!! This is not what should happens !!!");
            }
            @Override
            public void onFail(OPCommandFailureReason reason) {
                label.setText(reason.getDescription());
            }
        });
    }

    /**
     * Not a slot for a dispatch to view message
     */
    public void generateGivenIdIsNotASlotViewErrorMessage(final Panel container, final Label label) {
        final FlowPanel falseSlot = new FlowPanel();
        falseSlot.getElement().setId(DUMMY_ID);
        container.add(falseSlot);
        final OPViewProxy proxy = OPShell.getSlot(DUMMY_ID).getViewProxy();
        proxy.dispatchMessageToEmbeddedView(DUMMY_MESSAGE, null, new OPCommandCallback() {
            @Override
            public void onSuccess() {
                // no way we should get here
                label.setText("Error in the test, OPViewProxy found something that's not a slot !!! This is not what should happens !!!");
                container.remove(falseSlot);
            }
            @Override
            public void onFail(OPCommandFailureReason reason) {
                label.setText(reason.getDescription());
                container.remove(falseSlot);
            }
        });
    }
    
    /**
     * No view attached to view proxy
     */
    public void generateNoViewAttachedToProxyErrorMessage(final Panel container, final Label label) {
        final OPSlot slot = OPShell.getSlot(DUMMY_SERVICE, DUMMY_RESSOURCE, OPShell.ACTION_DISPLAY);
        container.add(slot);
        slot.getViewProxy().dispatchMessageToEmbeddedView(DUMMY_MESSAGE, null, new OPCommandCallback() {
            @Override
            public void onSuccess() {
                // no way we should get here
                label.setText("Error in the test, OPViewProxy found a view attached !!! This is not what should happens !!!");
                container.remove(slot);
            }
            @Override
            public void onFail(OPCommandFailureReason reason) {
                label.setText(reason.getDescription());
                container.remove(slot);
            }
        });
    }

}
